// miniprogram/pages/score/score.js
const cardimage=[{
        id: "0",
        score: "100",
        text1: "1元立减优惠券",
        text2: "领取后当天生效，生效15天",
        text3: "互斥券",
        price: "￥1",
        limit: "满5元可用",
        details: "查看详情",
        state: "立即兑换"
    },{
        id:"1",
        score: "200",
        text1: "2元立减优惠券",
        text2: "领取后当天生效，生效15天",
        text3: "互斥券",
        price: "￥2",
        limit: "满10元可用",
        details: "查看详情",
        state: "立即兑换"
    },{
        id:"2",
        score: "300",
        text1: "3元立减优惠券",
        text2: "领取后当天生效，生效15天",
        text3: "互斥券",
        price: "￥3",
        limit: "满20元可用",
        details: "查看详情",
        state: "立即兑换"
    },{
        id:"3",
        score: "400",
        text1: "4元立减优惠券",
        text2: "领取后当天生效，生效15天",
        text3: "互斥券",
        price: "￥4",
        limit: "满30元可用",
        details: "查看详情",
        state: "立即兑换"
    },
]
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // score: 150,
        cardimage
    },
    detail(){
        wx.navigateTo({
          url: '../detail/detail',
        })
    },
    judge(e){
        // console.log(e);
        let index = e.currentTarget.dataset.id;
        // console.log(index);
        
        if(this.data.score < cardimage[index].score){
            // console.log('11111111');
            wx.showToast({
              title: '您的积分不足，无法兑换',
              icon: 'none',
            })
        }else if(this.data.score >= cardimage[index].score){
            // console.log('11111111');
            let x = this.data.score
            let y = cardimage[index].score
            this.setData({
                score: x-y
            })
            wx.setStorageSync('score', this.data.score) //把积分存入storage
            let quanArr = wx.getStorageSync('quan') || [];
            wx.setStorageSync('quan', quanArr.concat(cardimage[index]))
            wx.showToast({
                title: '兑换成功，已存入优惠券',
                icon: 'none',
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let score=wx.getStorageSync('score');
        this.setData({
          score
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})